# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=gojq
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.12.17
pkgrel=1
pkgdesc='Pure go implementation of jq (mingw-w64)'
url='https://github.com/itchyny/gojq'
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-go" "${MINGW_PACKAGE_PREFIX}-cc" 'git')
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('86b8393d04cb40db09a8de96199f358c1e49bbd7d1ace5c95496cc7cc4102b3b')

build() {
  local _commit=$(zcat ${source[0]##*/} | git get-tar-commit-id)
  cd "${_realname}-${pkgver}"

  export CGO_LDFLAGS="$LDFLAGS"
  export CGO_CFLAGS="$CFLAGS"
  export CGO_CPPFLAGS="$CPPFLAGS"
  export CGO_CXXFLAGS="$CXXFLAGS"
  export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external -mod=readonly -modcacherw"

  mkdir -p "build-${MSYSTEM}"
  go build -o "build-${MSYSTEM}" \
    -ldflags="-X ${url#*//}/cli.revision=${_commit:0:7}" \
    "./cmd/gojq"
}

check() {
  cd "${_realname}-${pkgver}"

  go test ./...
}

package() {
  cd "${_realname}-${pkgver}"

  install -Dm755 "build-${MSYSTEM}"/gojq "${pkgdir}${MINGW_PREFIX}"/bin/gojq
  install -Dm755 _gojq "${pkgdir}${MINGW_PREFIX}"/usr/share/zsh/site-functions/_gojq
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
